﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MTQ.Model.PhoneModel
{
    public class Phone_UserInfo
    {
        public string token { get; set; }

        public Base_UserInfo UserInfo { get; set; }

        public List<string> UserControlProjectNumber { get; set; }

        public DateTime createTime { get; set; }

        public DateTime EndTime { get; set; }
    }

    public class Phone_Info
    {
        public static List<Phone_UserInfo> listPhone_UserInfo = new List<Phone_UserInfo>();
        public static string AddUserinfo(Base_UserInfo _UserInfo, List<string> ProjectNumber)
        {
            Phone_UserInfo userInfo = new Phone_UserInfo()
            {
                token = System.Guid.NewGuid().ToString(),
                createTime = System.DateTime.Now,
                EndTime = System.DateTime.Now.AddMinutes(20),
                UserControlProjectNumber = ProjectNumber,
                UserInfo = _UserInfo
            };

            if (listPhone_UserInfo == null)
            {
                listPhone_UserInfo = new List<Phone_UserInfo>();
            }

            listPhone_UserInfo.Add(userInfo);

            return userInfo.token;
        }
        public static bool CheckUserinfo(string token)
        {
            //if (listPhone_UserInfo == null)
            Phone_UserInfo phone_UserInfo = listPhone_UserInfo.Where(x => x.token == token).FirstOrDefault();
            if (phone_UserInfo == null)
            {
                return false;
            }
            phone_UserInfo.EndTime = System.DateTime.Now.AddMinutes(20);
            return true;
        }

        public static Phone_UserInfo GetUserinfo(string token) {
            Phone_UserInfo phone_UserInfo = listPhone_UserInfo.Where(x => x.token == token).FirstOrDefault();
            if (phone_UserInfo == null)
            {
                return null;
            }           
            return phone_UserInfo;

        }




        public static void Remove()
        {
            var list = listPhone_UserInfo.Where(x => x.EndTime < System.DateTime.Now).ToList();
            foreach (var item in list)
            {
                listPhone_UserInfo.Remove(item);
            }



        }

    }
}
